package cn.tedu.autoshow.website.controller;

import cn.tedu.autoshow.commons.pojo.vo.PageData;
import cn.tedu.autoshow.commons.security.LoginPrincipal;
import cn.tedu.autoshow.commons.web.JsonResult;
import cn.tedu.autoshow.website.pojo.param.TicketOrderAddNewParam;
import cn.tedu.autoshow.website.pojo.param.TicketOrderUpdaeInfoParam;
import cn.tedu.autoshow.website.pojo.vo.TicketOrderListItemVO;
import cn.tedu.autoshow.website.pojo.vo.TicketOrderStandardVO;
import cn.tedu.autoshow.website.service.ITicketOrderService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;


@RestController
@RequestMapping("/ticketOrders")
@Validated
@Slf4j
@Api(tags = "01. 购票订单管理模块")
public class TicketOrderController {
    @Autowired
    ITicketOrderService service;
    
    @PostMapping("/add-new")
    @ApiOperation("新增购票订单")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid TicketOrderAddNewParam ticketOrderAddNewParam ,
                             @ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal){
        ticketOrderAddNewParam.setUserId(loginPrincipal.getId());
        service.addNew(ticketOrderAddNewParam);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除购票订单")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "购票订单ID", required = true, dataType = "Long")
    public JsonResult deleteById(
            @Range(min = 1, message = "购票订单ID不合法") @PathVariable Long id,
            @ApiIgnore HttpSession session){

        service.deleteById(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("根据ID修改购票订单")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "购票订单ID", required = true, dataType = "Long")
    public JsonResult deleteById(
            @Range(min = 1, message = "购票订单ID不合法") @PathVariable Long id,
            @Valid TicketOrderUpdaeInfoParam ticketOrderUpdaeInfoParam, @ApiIgnore HttpSession session){

        service.updateInfoById(id, ticketOrderUpdaeInfoParam);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询购票订单详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "购票订单ID", required = true, dataType = "Long")
    })
    public JsonResult getStandardById(
            @Range(min = 1, message = "根据ID查询购票订单失败，请提交合法的ID值！")
            @RequestParam Long id){
        TicketOrderStandardVO ticketOrderStandardVO = service.getStandardById(id);
        return JsonResult.ok(ticketOrderStandardVO);
    }

    @GetMapping("")
    @ApiOperation("查询购票订单列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码值", paramType = "query"),
            @ApiImplicitParam(name = "queryType", value = "查询类型", example = "all")
    })
    public JsonResult list(
            @Range(min = 1, message = "查询相册列表失败，请提供正确的页码值!") Integer pageNum,
            String queryType){
        if(pageNum == null || pageNum < 1){
            pageNum = 1;
        }
        PageData<TicketOrderListItemVO> pageData;
        if("all".equals(queryType)){
            pageData = service.list(pageNum, Integer.MAX_VALUE);
        }else{
            pageData = service.list(pageNum);
        }
        return JsonResult.ok(pageData);
    }

    @GetMapping("/user/{userId:[0-9]+}")
    @ApiOperation("查询购票订单列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码值", paramType = "query"),
            @ApiImplicitParam(name = "queryType", value = "查询类型", example = "all")
    })
    public JsonResult list(
            @Range(min = 1, message = "用户ID不合法") @PathVariable Long userId,
            @Range(min = 1, message = "查询相册列表失败，请提供正确的页码值!") Integer pageNum,
            String queryType){
        if(pageNum == null || pageNum < 1){
            pageNum = 1;
        }
        PageData<TicketOrderListItemVO> pageData;
        if("all".equals(queryType)){
            pageData = service.listByUserId(userId, pageNum, Integer.MAX_VALUE);
        }else{
            pageData = service.listByUserId(userId, pageNum);
        }
        return JsonResult.ok(pageData);
    }
}
